package edu.ptit.jad.preprocess.app;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.List;
import java.util.Map;

import edu.ptit.jad.configuration.CONSTANTS;
import edu.ptit.jad.data.DataAccess;
import edu.ptit.jad.model.Ad;
import edu.ptit.jad.model.Content;

public class AppGenerateWordCount {

	public static void main(String[] args) throws Exception {

		File file = new File("data/input/ads_wordcount.jad");
		BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
				new FileOutputStream(file), "UTF8"));

		// read vocals, index

		Map<String, Integer> vobs = DataAccess.getInstance().getVocabularies();

		List<String> docs = DataAccess.getInstance().getContents(
				CONSTANTS.FILE_DATA_ADS_PRE);

		// each documents , build term of frequence and write to file

		int index = 1;
		Content content = new Content();

		System.err.println(docs.size());
		for (String doc : docs) {

			Map<String, Integer> freq = content.buildTermFrequencyMap(doc);

			for (Map.Entry<String, Integer> e : freq.entrySet()) {

				if (vobs.get(e.getKey()) != null) {
					out.write(index + " " + vobs.get(e.getKey()) + " "
							+ e.getValue());
					out.write("\n");
				}
			}

			index++;

		}

		out.close();

	}

}
